#!/bin/sh
# PCP QA Test No. 1072
# Exercise pmrep archive creation.
#
# Copyright (c) 2016 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

if [ $PCP_PLATFORM = freebsd ]
then
    # workaround Python class teardown problem
    # AttributeError: 'NoneType' object has no attribute ...
    #
    version=`uname -rs | sed -e 's/FreeBSD //'`
    case "$version"
    in
	14.[0-3]-*)
	    _notrun "Python teardown botch"
	    # NOTREACHED
	    ;;
    esac
fi

status=1        # failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_live_filter()
{
    sed \
	-e "s;$tmp;TMP;g" \
        -e "s/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/HH:MM:SS/g" \
    #end
}

_path_filter()
{
    sed \
        -e "s#$here#QAPATH#g" \
    #end
}

#  timezone: :Australia/Melbourne (reporting, current is AEDT-11)
#  this is not deterministic .... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_archive_filter()
{
    sed \
	-e "s;$tmp;TMP;g" \
	-e '/timezone/s/ (reporting.*)//' \
    #end
}

_pid_filter()
{
    sed \
	-e "s;$tmp;TMP;g" \
        -e '/archive:/s/archive: .*/archive: temp-archive/' \
        -e '/Recording/s/-[0-9][0-9]*//' \
    #end
}

# V3 (nsec) -> V2 (usec) timestamps
_dumplog_filter()
{
    sed \
	-e '/\.......000 [0-9][0-9]* metric/s/000 / /' \
	-e '/\.......000 [0-9][0-9]* instance/s/000 / /' \
    # end
}

log="--archive $here/archives/rep"
log2="--archive $here/archives/20130706 -O 10m"

# real QA test starts here
echo "== pmrep archive reporting and creation"
echo "Writing output from the original archives to files"
metrics1=$(pminfo $log         | grep -v event\. | LC_COLLATE=POSIX sort | tr '\n' ' ')
metrics2=$(pminfo $log2 kernel | grep -v event\. | LC_COLLATE=POSIX sort | tr '\n' ' ')
pmrep -u -s 5 -p -P 4 -w 12 -x -z $log  $metrics1 | _path_filter | _archive_filter >$tmp.a1-out
pmrep -u -s 5 -p -P 4 -w 12 -x -z $log2 $metrics2 | _path_filter | _archive_filter >$tmp.a2-out
pmrep -u -s 5 -o archive -F $tmp.new_archive1 -z $log  $metrics1 | _pid_filter
pmrep -u -s 5 -o archive -F $tmp.new_archive2 -z $log2 $metrics2 | _pid_filter
echo "Writing output from the created archives to files"
pmrep -u -s 5 -p -P 4 -w 12 -x -z -a $tmp.new_archive1 $metrics1 | \
  _path_filter | _archive_filter | _pid_filter >$tmp.a1-out-new
pmrep -u -s 5 -p -P 4 -w 12 -x -z -a $tmp.new_archive2 $metrics2 | \
  _path_filter | _archive_filter | _pid_filter >$tmp.a2-out-new
echo "Diff of outputs start (only path / minor timestamp diff expected):"
diff -u $tmp.a1-out $tmp.a1-out-new | grep -v +++ | grep -v -- ---
diff -u $tmp.a2-out $tmp.a2-out-new | grep -v +++ | grep -v -- ---
echo "Diff of outputs end"

echo "=== pmrep archive creation with filtering"
pmrep -u -s 5 -o archive -F $tmp.new_archive3 -z $log2 -i 'sda' -i '.*sdc.*' \
   disk.dev.write_bytes | _pid_filter
pmrep -u -s 5 -p -P 4 -w 12 -x -z -a $tmp.new_archive3 disk.dev.write_bytes | \
  _path_filter | _archive_filter | _pid_filter

echo "=== pmrep archive creation with discrete values"
pmrep -u -s 5 -o archive -F $tmp.new_archive4 -z $log hinv.machine mem.util.used | _pid_filter
pmrep -u -a $tmp.new_archive4 -z hinv.machine mem.util.used
pmdumplog -z -dim $tmp.new_archive4 | _dumplog_filter

echo "=== pmrep archive creation with scaled values"
pmrep -u -s 5 -o archive -F $tmp.new_archive5 -z $log2 -i sda,sdc disk.dev.write_bytes,,,MB | _pid_filter
# Using -r to display the unit in the archive
pmrep -u -a $tmp.new_archive5 -z -r disk.dev.write_bytes
pmdumplog -z -dim $tmp.new_archive5 | _dumplog_filter

rm -f $tmp.a1-out* $tmp.a2-out* $tmp.new_archive*

# success, all done
echo "== done"
status=0
exit
